package com.flushbonading.service.impl;

import com.flushbonading.mapper.RoleMenuMapper;
import com.flushbonading.pojo.Role;
import com.flushbonading.mapper.RoleMapper;
import com.flushbonading.pojo.dto.RoleDTO;
import com.flushbonading.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zlx
 * @since 2021-04-07
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    /**
     * 通过role_id返回role名
     * @param role_id
     * @return 返回role名
     * **/
    @Override
    public String getNameByRoleId(int role_id) {
        return  roleMapper.getNameByRoleId(role_id);
    }

    @Override
    public List<RoleDTO> getAllRoleMessage() {
        return roleMapper.getAllRoleMessage();
    }

    @Override
    public int updateRole(RoleDTO roleDTO) {
        return roleMapper.updateRole(roleDTO);
    }

    @Override
    public int insertRole(RoleDTO roleDTO) {
        int lastID = roleMapper.getLastID();
        int j = 1;
        if (lastID>0){
            roleDTO.setRole_id(lastID+1);
            j = roleMapper.insertRole(roleDTO);
        }
        return j;
    }

    @Override
    public int deleteRoleByID(int role_id) {
        int i = roleMenuMapper.deleteRoleMenuByID(role_id);
        int j = -1;
        if (i>=0){
            j = roleMapper.deleteRoleByID(role_id);
        }
        return j;
    }

    @Override
    public List<RoleDTO> searchRole(String role_id, String role_name) {
        return roleMapper.searchRole(role_id,role_name);
    }

    @Override
    public List<RoleDTO> getAllRoleName() {
        return roleMapper.getAllRoleName();
    }
}
